package com.yupi.maker.template;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.yupi.maker.template.enums.FileFilterRangeEnum;
import com.yupi.maker.template.enums.FileFilterRuleEnum;
import com.yupi.maker.template.model.FileFilterConfig;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/9/21 17:57
 * @注释
 */
public class FileFilter {

    /**
     * 对目录进行遍历
     * @param filePath
     * @param fileFilterConfigList
     * @return
     */
    public static List<File> doFilter(String filePath, List<FileFilterConfig> fileFilterConfigList) {
        return FileUtil.loopFiles(filePath).stream()
                .filter(file -> doSingleFileFilter(fileFilterConfigList,file))
                .collect(Collectors.toList());
    }

    /**
     * 单个文件过滤
     * @param fileFilterConfigList
     * @param file
     * @return
     */
    public static boolean doSingleFileFilter(List<FileFilterConfig> fileFilterConfigList, File file) {
        String fileName=file.getName();
        String fileContent = FileUtil.readUtf8String(file);

        //校验规则的结果
        boolean result=true;
        if (CollUtil.isEmpty(fileFilterConfigList)) {
            return true;
        }
        //遍历规则
        for (FileFilterConfig fileFilterConfig : fileFilterConfigList) {
            String range = fileFilterConfig.getRange();
            String value = fileFilterConfig.getValue();
            String rule = fileFilterConfig.getRule();

            //根据文件名称过滤还是根据文件内容进行过滤
            FileFilterRangeEnum fileFilterRangeEnum = FileFilterRangeEnum.getEnumByValue(range);
            if (fileFilterRangeEnum == null) {
                continue;
            }
            String content=fileName;
            switch (fileFilterRangeEnum){
                case FILE_NAME:
                    content=fileName;
                    break;
                case FILE_CONTAINS:
                    content=fileContent;
                    break;
                default:
            }
            //过滤的规则
            FileFilterRuleEnum fileFilterRuleEnum = FileFilterRuleEnum.getEnumByValue(rule);
            if(fileFilterRuleEnum==null){
                continue;
            }
            switch (fileFilterRuleEnum){
                case CONTAINS:
                    result = content.contains(value);
                    break;
                case STARTS_WITH:
                    result = content.startsWith(value);
                    break;
                case ENDS_WITH:
                    result = content.endsWith(value);
                    break;
                case REGEX:
                    result = content.matches(value);
                    break;
                case EQUALS:
                    result = content.equals(value);
                    break;
                default:
            }
            //有一个不满足就直接返回
            if(!result){
                return false;
            }
        }
        //都满足
        return true;
    }
}
